package components.game
{
	import SEApp.boxes.albums.se_previewItem;
	
	import UIFree.controls.uif_label;
	import UIFree.uif_component;
	import UIFree.uif_ecomponent;
	
	import components.clt_component;
	import components.clt_layer;
	import components.pj_hscrollArea;
	
	import flash.geom.Rectangle;

	public class clt_gamePart extends clt_component {
		protected var _itemsLayer: pj_hscrollArea;
		private const rows: int = 1;
		private var _filterList: Array;
		private var _gameItemClass: Class;
		
		public function clt_gamePart(parent: uif_ecomponent, ax:int, ay: int, 
									awidth:int, aheight: int, aTitle: String,
									a_gameItemClass: Class=null) {
			super(parent, ax, ay, awidth, aheight, 'block');
			_gameItemClass = (a_gameItemClass!=null)?a_gameItemClass:clt_gameItem;
			var inner: Rectangle = getInner();
			(new uif_label(this, inner.x, inner.y, awidth, 20, 'block-title')).text = aTitle;
			_itemsLayer = new pj_hscrollArea(this, inner.x, inner.y + 20, inner.width, inner.height - 20, 'items', 27, false);
			visible = false;
		}
		
		public function get itemsLayer(): pj_hscrollArea {
			return _itemsLayer;
		}
		
		public function assignList(a_list: Array): void {
			visible = a_list.length > 0;
			if (a_list.length > 0) {
				var tmp: Object = _itemsLayer.selectItem?_itemsLayer.selectItem.object:null;
				var item_width: int = _itemsLayer.calcFrameWidth(rows);
				_filterList = a_list.concat([]);
				while (_filterList.length < (Math.floor(_itemsLayer.displayRect.width / item_width) * rows)) _filterList.push(null);
				_itemsLayer.assignList(_filterList, item_width, createItemFunc, rows);
				if (tmp) {
					_itemsLayer.items.forEach(function(item: Object, i:int, arr: Array): void {
						if (item.object && (item.object.id == tmp.id))
							_itemsLayer.selectIndex = i;
					});
				}
			}
		}
		
		public function get list(): Array {
			return _filterList;
		}
		
		protected function createItemFunc(_scroll: uif_component, inner: Rectangle, index: int): se_previewItem {
			return new _gameItemClass(_scroll, inner.x, inner.y, inner.width, inner.height, _filterList[index], 'userPhoto');
		}
	}
}